//
//  KRAlbumView.m
//  cookBook
//
//  Created by kuroken on 16/1/11.
//  Copyright © 2016年 kuroken. All rights reserved.
//

#import "KRAlbumView.h"
#import <UIImageView+WebCache.h>
#import <UIButton+WebCache.h>
#import <PureLayout.h>
#import "KRConst.h"
#import "KRAlbumList.h"

NSUInteger const KRAlbumHeight = 230;


@interface KRAlbumBtn : UIButton

@property (nonatomic, weak) UIImageView *imgView;
@property (nonatomic, weak) UILabel *titleView;
@property (nonatomic, weak) UILabel *recipeView;
@end

@implementation KRAlbumBtn

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupSubview];
    }
    return self;
}

- (void)setupSubview
{
    UIImageView *imgView = [[UIImageView alloc] init];
    [imgView setContentMode:UIViewContentModeScaleAspectFill];
    [imgView setClipsToBounds:YES];
    [self addSubview:imgView];
    self.imgView = imgView;
    
    UILabel *recipeView = [[UILabel alloc] init];
    [recipeView setTextColor:[UIColor whiteColor]];
    [recipeView setFont:[UIFont systemFontOfSize:14]];
    [self addSubview:recipeView];
    self.recipeView = recipeView;
    
    UILabel *titleView = [[UILabel alloc] init];
    [titleView setNumberOfLines:0];
    [titleView setFont:[UIFont systemFontOfSize:15]];
    [titleView setTextColor:[UIColor blackColor]];
    [titleView setLineBreakMode:NSLineBreakByCharWrapping];
    [self addSubview:titleView];
    self.titleView = titleView;
    

}

- (void)layoutSubviews
{
    CGFloat padding = 2;
    CGFloat imgX = padding;
    CGFloat imgY = imgX;
    CGFloat imgW = self.frame.size.width - imgX - imgY;
    CGFloat imgH = imgW;
    self.imgView.frame = CGRectMake(imgX, imgY, imgW, imgH);
    
    CGFloat recipeW = imgW;
    CGFloat recipeH = 20;
    CGFloat recipeX = imgX;
    CGFloat recipeY = CGRectGetMaxY(self.imgView.frame) - recipeH;
    self.recipeView.frame = CGRectMake(recipeX, recipeY, recipeW, recipeH);
    
    CGFloat titleX = imgX;
    CGFloat titleY = CGRectGetMaxY(self.imgView.frame);
    CGFloat titleW = imgW;
    CGFloat titleH = self.height - titleY;
    self.titleView.frame = CGRectMake(titleX, titleY, titleW, titleH);
}

@end

@interface KRAlbumView ()

@property (nonatomic, strong) NSMutableArray<KRAlbumBtn *> *albumBtnArray;

@end

@implementation KRAlbumView

- (NSMutableArray<KRAlbumBtn *> *)albumBtnArray
{
    if (!_albumBtnArray) {
        self.albumBtnArray = [[NSMutableArray<KRAlbumBtn *> alloc] init];
    }
    return _albumBtnArray;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupSubview];
    }
    return self;
}

- (void)setupSubview
{
    for (NSInteger i = 0; i < 3; i++) {
        KRAlbumBtn *albumBtn = [[KRAlbumBtn alloc] init];
        [self addTargetWithBtn:albumBtn];
        [self addSubview:albumBtn];
        [self.albumBtnArray addObject:albumBtn];
    }
}

#warning 每个controlEvent都很重要！
- (void)addTargetWithBtn:(KRAlbumBtn *)albumBtn
{
    [albumBtn addTarget:self action:@selector(albumBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [albumBtn addTarget:self action:@selector(setBtnHighlightedColor:) forControlEvents:UIControlEventTouchDown | UIControlEventTouchDragInside];
    [albumBtn addTarget:self action:@selector(setBtnNormalColor:) forControlEvents:UIControlEventTouchUpInside |UIControlEventTouchDragOutside | UIControlEventTouchCancel];
}

- (void)layoutSubviews
{
    CGFloat margin = 10;
    CGFloat width = (KRScreenWidth - margin * 2) / 3;
    CGFloat height = width * 1.5;
    for (NSInteger i = 0; i < 3; i++) {
        CGFloat x = margin + width * i;
        CGFloat y = 0;
        self.albumBtnArray[i].frame = CGRectMake(x, y, width, height);
    }
}

- (void)setAlbumList:(NSArray<KRAlbumList *> *)albumList
{
    _albumList = albumList;
    
    for (NSInteger i = 0; i < 3; i++) {
        [self.albumBtnArray[i].imgView sd_setImageWithURL:[NSURL URLWithString:albumList[i].img] placeholderImage:[UIImage imageNamed:@"default_p280"]];
        [self.albumBtnArray[i].titleView setText:[NSString stringWithFormat:@"%@\n\n\n\n", albumList[i].title]];
        [self.albumBtnArray[i].recipeView setText:[NSString stringWithFormat:@" %@", albumList[i].recipe]];
        
        self.albumBtnArray[i].tag = i;
    }
}

- (void)setBtnHighlightedColor:(KRAlbumBtn *)btn
{
    [btn setBackgroundColor:KRColorRGB(208)];
}

- (void)setBtnNormalColor:(KRAlbumBtn *)btn
{
    [btn setBackgroundColor:[UIColor clearColor]];
}

- (void)albumBtnClick:(KRAlbumBtn *)btn
{
    if ([self.delegate respondsToSelector:@selector(albumViewDidClickAlbumBtn:withAlbumID:)]) {
        NSInteger i = btn.tag;
        [self.delegate albumViewDidClickAlbumBtn:btn withAlbumID:self.albumList[i].url_ID];
    }
    
}

@end
